package com.demo.springboothexadecimalconversion.hextobyte;

/**
 *
 * 二进制数据转化为16进制字符串
 *
 * @author Hanson
 * @date 2022/10/11 11:21
 */
public class bytesToHexString {

    public static void main(String[] args) {
        String s1 = "SC/UP/OTA/D/123";
        String s2 = "HCR/UP/D/123";
        String substring1 = s1.substring(0,s1.lastIndexOf("/"));
        String substring2 = s2.substring(0,s2.lastIndexOf("/"));
        System.out.println(substring1);
        System.out.println(substring2);
    }

    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);

            stringBuilder.append(i + ":");

            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv + ";");
        }
        return stringBuilder.toString();
    }
}
